package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayComment;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/16
 * @Description：
 */
@Service
public class SundayCommentService extends BaseMongoService<SundayComment>{
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayComment> getEntityClass() {
        return SundayComment.class;
    }

    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayMemberService memberService;

    /**
     * 分页查询评论
     * @param request
     * @return
     */
    public PageResult<SundayComment> select(HttpServletRequest request,Integer status){

        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        if(status!=null){
            param.put("status", status);
        }
        PageResult<SundayComment> commentPageResult = super.containpageQuery(request, null, param);
        List<SundayComment> data = commentPageResult.getData();
        if(ListUtil.isNull(data)){
            return commentPageResult;
        }
        List<Long > productIds=new ArrayList<>();
        List<Long> memberIds=new ArrayList<>();
        for (SundayComment comment:data) {
            productIds.add(comment.getProductId());
            memberIds.add(comment.getMemberId());
        }
        Map _param=new HashMap<>();
        _param.put("ids",productIds);
        List<SundayProduct> products = productService.select(_param);
        _param.clear();
        _param.put("isDeleted",SundayCommonConstants.NO);
        _param.put("id in",memberIds);
        List<SundayMember> members = memberService.list(_param);
        for (SundayComment comment:data) {
            for (SundayMember member:members) {
                if(member.getId()==comment.getMemberId().longValue()){
                    comment.setMemberName(member.getNickName());
                    break;
                }
            }
            for (SundayProduct product:products) {
                if(product.getId().equals(comment.getProductId())){
                    comment.setProductName(product.getName());
                    break;
                }
            }
        }
        return commentPageResult;
    }

    public void setStatus(Long id,Integer status){
        super.update(id,"status",status);
    }
}
